<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="filter-form">
      <el-form-item label="医生">
        <el-select v-model="searchForm.doctorId" placeholder="请选择" clearable>
          <el-option v-for="item in doctorList" :key="item.doctorId" :label="item.doctorName" :value="item.doctorId" />
        </el-select>
      </el-form-item>
      <el-form-item label="科室">
        <el-select v-model="searchForm.deptId" placeholder="请选择" clearable>
          <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择" clearable>
          <el-option label="出诊" :value="1" />
          <el-option label="停诊" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="问诊类型">
        <el-select v-model="searchForm.fuwu" placeholder="请选择" clearable>
          <el-option v-for="item in serviceList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="searchForm.dateRange"
          type="daterange"
          range-separator="—"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchList">查询</el-button>
        <el-button @click="handleBatch" style="margin-left: 10px;">批量操作</el-button>
        <el-button type="primary" @click="handleAdd" style="margin-left: 10px;">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%; margin-top: 20px;">
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="scheduleId" label="ID" />
      <el-table-column prop="doctorName" label="医生名称" />
      <el-table-column prop="deptName" label="科室" />
      <el-table-column prop="fuwu" label="服务" />
      <el-table-column prop="scheduleDate" label="出诊日期" />
      <el-table-column prop="wubie" label="午别" />
      <el-table-column prop="startTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间" />

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.status == 1 ? '#67C23A' : '#F56C6C' }">
            {{ scope.row.status == 1 ? '出诊' : '停诊' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间" />
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="handleStop(scope.row)" v-if="scope.row.status == 1" style="color: #F56C6C;">停诊</el-button>
          <el-button type="text" size="mini" @click="handleEnable(scope.row)" v-else style="color: #67C23A;">出诊</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; text-align: right;"
      background
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      :page-size="searchForm.pageSize"
      :current-page="searchForm.pageNum"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
import axios from 'axios';
export default {
  name: 'ScheduleManage',
  data() {
    return {
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        doctorId: '',
        deptId: '',
        status: '',
        fuwu: '',
        dateRange: []
      },
      doctorList: [],
      deptList: [],
      serviceList: [
        { id: 1, name: '图文问诊' },
        { id: 2, name: '视频问诊' }
      ],
      tableData: [],
      total: 0
    };
  },
  created() {
    this.fetchDoctorList();
    this.fetchDeptList();
    this.fetchList();
  },
  methods: {
    fetchDoctorList() {
      axios.get('/doctors/list').then(res => {
        this.doctorList = res.data || [];
      });
    },
    fetchDeptList() {
      axios.get('/departments/list', { params: { pageNum: 1, pageSize: 100 } }).then(res => {
        this.deptList = res.data.records || [];
      });
    },
    fetchList() {
      axios.get('/schedules/list', {
        params: {
          pageNum: this.searchForm.pageNum,
          pageSize: this.searchForm.pageSize,
          doctorId: this.searchForm.doctorId,
          deptId: this.searchForm.deptId,
          status: this.searchForm.status,
          fuwu: this.searchForm.fuwu,
          startTime: this.searchForm.dateRange[0] || '',
          endTime: this.searchForm.dateRange[1] || ''
        }
      }).then(res => {
        // 兼容 records/data 两种格式
        if (Array.isArray(res.data)) {
          this.tableData = res.data;
          this.total = res.data.length;
        } else if (res.data && Array.isArray(res.data.records)) {
          this.tableData = res.data.records;
          this.total = res.data.total || res.data.records.length;
        } else if (res.data && Array.isArray(res.data.data)) {
          this.tableData = res.data.data;
          this.total = res.data.total || res.data.data.length;
        } else {
          this.tableData = [];
          this.total = 0;
        }
      });
    },
    handlePageChange(page) {
      this.searchForm.pageNum = page;
      this.fetchList();
    },
    handleSizeChange(size) {
      this.searchForm.pageSize = size;
      this.fetchList();
    },
    handleAdd() {
      this.$message.info('点击了新增');
      // 跳转到新增页面或弹窗
    },
    handleBatch() {
      this.$message.info('点击了批量操作');
      // 批量操作逻辑
    },
    handleEdit(row) {
      this.$message.info('点击了编辑: ' + row.doctorName);
      // 跳转到编辑页面或弹窗
    },
    handleStop(row) {
      this.$message.info('点击了停诊: ' + row.doctorName);
      // 调用停诊接口
    },
    handleEnable(row) {
      this.$message.info('点击了出诊: ' + row.doctorName);
      // 调用出诊接口
    }
  }
};
</script>

<style scoped>
.filter-form {
  margin-bottom: 20px;
}
</style>